// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Riolinx.SocialConnekt.SecurityTokenService.Services
{
    using System.Web;

    public class FederationContext : IFederationContext
    {
        public string Realm
        {
            get { return HttpUtility.UrlDecode(this.GetValue("wtrealm")); }
            set { this.SetValue("wtrealm", HttpUtility.UrlEncode(value)); }
        }

        public string WebContext
        {
            get { return HttpUtility.UrlDecode(this.GetValue("wctx")); }
            set { this.SetValue("wctx", HttpUtility.UrlEncode(value)); }
        }

        public string OriginalUrl
        {
            get { return HttpUtility.UrlDecode(this.GetValue("originalUrl")); }
            set {
                if (this.GetValue("originalUrl") != value)
                {
                    HttpContext.Current.Request.Cookies.Clear();
                    //var cookie = HttpContext.Current.Request.Cookies.Get("FederationContext");
                }
                
                this.SetValue("originalUrl", HttpUtility.UrlEncode(value)); }
        }

        public string IssuerName
        {
            get { return this.GetValue("issuerName"); }
            set { this.SetValue("issuerName", value); }
        }

        private static HttpCookie FederationCookie
        {
            get
            {
                var cookie = HttpContext.Current.Request.Cookies.Get("FederationContext");

                if (cookie == null)
                {
                    cookie = new HttpCookie("FederationContext");
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }

                return cookie;
            }
        }

        public string GetValue(string key)
        {
            return FederationCookie.Values[key];
        }

        public void SetValue(string key, string value)
        {
            HttpCookie cookie = FederationCookie;
            cookie.Values[key] = value;
            //HttpContext.Current.Request.Cookies.Set(cookie);
            HttpContext.Current.Response.Cookies.Set(cookie);
        }
    }
}